//
//  MutipChooseView.m
//  Select
//
//  Created by dihuijun on 17/3/16.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "MutipChooseView.h"
#import "SignalTableViewCell.h"


#define HeaderHeight 50
#define CellHeight 50


static NSString *IDCell = @"mulTableCell";





@implementation MutipChooseView






/**  有header的初始化  */
+ (MutipChooseView *)shareMutipTableViewWithFrame:(CGRect)frame HeaderTitle:(NSString *)title{
    MutipChooseView *share = [[MutipChooseView alloc]initWithFrame:frame HaveHeader:YES HeaderTitle:title];
    return share;
}
//自定义初始化
- (instancetype)initWithFrame:(CGRect)frame HaveHeader:(BOOL)ifHave HeaderTitle:(NSString *)title{
    
    self = [super init];
    if (self) {
        
        self.frame = frame;
        //创建表格
        [self setupTableView];
        
        //是否有头部
        if (ifHave) {
            
            UIView *view = [self setupHeaderView_HeaderTitle:title];
            
            self.tableView.tableHeaderView  = view;
        }
    }
    return self;
}

//创建表格
- (void)setupTableView
{
    //初始化数组
    _chooseArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[SignalTableViewCell class] forCellReuseIdentifier:IDCell];
    [self addSubview:self.tableView];
}












//有头部
- (UIView *)setupHeaderView_HeaderTitle:(NSString *)title
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HeaderHeight)];
    
    //1.title标题
    UILabel *headerTitleLabel = [UILabel new];
    headerTitleLabel.text = title;
    [headerView addSubview:headerTitleLabel];
    //布局
    [headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(15);
        make.top.equalTo(headerView.mas_top).offset(0);
        make.height.mas_equalTo(headerView.mas_height);
    }];
    
    //2.按钮
    UIButton *chooseIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseIcon.tag = 10;
    [chooseIcon setImage:[UIImage imageNamed:@"table_UnSelect"] forState:UIControlStateNormal];
    [chooseIcon setImage:[UIImage imageNamed:@"table_Selected"] forState:UIControlStateSelected];
    
//    chooseIcon.backgroundColor = [UIColor orangeColor];
    //是否可触摸
    chooseIcon.userInteractionEnabled = YES;
    
    [headerView addSubview:chooseIcon];
    //布局
    [chooseIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headerTitleLabel.mas_right).offset(10);
        make.right.equalTo(headerView.mas_right).offset(0);
        make.top.equalTo(headerView.mas_top);
        make.height.mas_equalTo(headerView.mas_height);
        make.width.mas_equalTo(50);
    }];
    
    ///3.整行按钮选择
    UIButton *chooseButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseButton.frame      = CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height);
    //点击
    [chooseButton addTarget:self action:@selector(ChooseAllClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:chooseButton];
    
    
    return headerView;
}
//3.整行按钮选择
- (void)ChooseAllClick:(UIButton *)button
{
    _isAllSelectSwitch = YES;
    
    _isAllSelected = !_isAllSelected;
    ///根据tag取出chooseIcon
    UIButton *chooseIcon = (UIButton *)[self.tableView.tableHeaderView viewWithTag:10];
    chooseIcon.selected = _isAllSelected;
    
    //全部选中
    if (_isAllSelected) {
        [_chooseArray removeAllObjects];
        //把所有数据添加上去
        [_chooseArray addObjectsFromArray:_datasource];
    }else{
        [_chooseArray removeAllObjects];
        
    }
    //刷新表格
    [self.tableView reloadData];
    
    _blockMore(@"All",_chooseArray);
    
    
    
    
    
}
/**  无header */
+ (instancetype)ShareMutipTableViewWithNoHeaderFrame:(CGRect)frame{
    
    static MutipChooseView *shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[MutipChooseView alloc]initWithFrame:frame HaveHeader:NO HeaderTitle:nil];
    });
    
  
    return shareInstance;
}














#pragma mark ---- <UITableViewDelegate,UITableDatasource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SignalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDCell];
    cell.titleLabel.text = self.datasource[indexPath.row];
    ///如果全部选中
    if (_isAllSelectSwitch) {
        [cell updateCellWithState:_isAllSelected];
        if (indexPath.row==self.datasource.count-1) {
            _isAllSelectSwitch = NO;
        }
    }
    
    
    return cell;
    
}
//选择
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SignalTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell updateCellWithState:!cell.isSelected];
    
    //如果被选中
    if (cell.isSelected) {
        [self.chooseArray addObject:cell.titleLabel.text];
        
    }else{
        [self.chooseArray removeObject:cell.titleLabel.text];
    }
    
    ///如果选择的东西的条数 小于总条数 （用于取消全选按钮的状态，让它变成没选中状态）
    if (_chooseArray.count <_datasource.count) {
        _isAllSelected = NO;
        UIButton *chooseIcon = (UIButton *)[self.tableView.tableHeaderView viewWithTag:10];
        chooseIcon.selected = _isAllSelected;
    }else{
        _isAllSelected = YES;
        UIButton *chooseIcon = (UIButton *)[self.tableView.tableHeaderView viewWithTag:10];
        chooseIcon.selected = _isAllSelected;
    }
    _blockMore(cell.titleLabel.text,_chooseArray);
}

/**  刷新 */
- (void)reloadData{
    [self.tableView reloadData];
}






@end
